package py.com.fms.session;

import java.util.Collection;
import java.util.Iterator;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
import org.richfaces.component.html.HtmlExtendedDataTable;
import org.richfaces.model.selection.SimpleSelection;

import py.com.fms.exception.CommonException;
import py.com.fms.fms.bean.SupplierBean;
import py.com.fms.ven.bean.CustomerBean;
import py.com.fms.fms.persistence.services.SupplierServices;
import py.com.fms.ven.persistence.services.CustomerServices;

@Name("selectHolder")
@Scope(ScopeType.SESSION)
public class modalSelectHolder {

	@In("#{serviceSqlMap}")
	private ServiceSqlMap sqlMap;
	@Logger
	private Log log;
	
	// Suppliers
	private SupplierBean supplierPickData = new SupplierBean();
	private Collection<SupplierBean> supplierList;
    private HtmlExtendedDataTable supplierPickTable;
    private SimpleSelection supplierPickSelection;
    
    // Customers
	private CustomerBean customerPickData = new CustomerBean();
	private Collection<CustomerBean> customerList;
	
	public Collection<SupplierBean> supplierPickList() {
		try {
			if(sqlMap == null){
				sqlMap = new ServiceSqlMap();
			}
			// Retrieving Suppliers
			this.cleanData();
			supplierPickData = new SupplierBean();
			SupplierServices spSrv = new SupplierServices(this.sqlMap.getConfig());
			supplierList = spSrv.getSupplierPickList(supplierPickData.getCod());

		} catch (CommonException e) {
			this.log.error(e);
		}
		return supplierList;
	}
	
	
	public void takeSelection() {
        Iterator<Object> iterator = supplierPickSelection.getKeys();
        while (iterator.hasNext()) {
            Object key = iterator.next();
            supplierPickTable.setRowKey(key);
            if (supplierPickTable.isRowAvailable()) {
            	supplierPickData = (SupplierBean) supplierPickTable.getRowData();
                System.out.println("SELECTED SUPPLIER: "+supplierPickData.getCod());
            }
        }
    }
	
	public Collection<CustomerBean> customerPickList() {
		try {
			if(sqlMap == null){
				sqlMap = new ServiceSqlMap();
			}
			// Retrieving Suppliers
			this.cleanData();
			customerPickData = new CustomerBean();
			CustomerServices ctSrv = new CustomerServices(this.sqlMap.getConfig());
			customerList = ctSrv.getCustomerPickList(customerPickData.getCod());

		} catch (CommonException e) {
			this.log.error(e);
		}
		return customerList;
	}

	public void cleanData(){
		System.out.println("Cleaning Modal Select Data");
		supplierPickData = null;
		supplierList = null;
		supplierPickTable.clearRequestKeysSet();
	}
	
	
	// ACCESSORS AND MUTATORS
	
	public ServiceSqlMap getSqlMap() {
		return sqlMap;
	}


	public void setSqlMap(ServiceSqlMap sqlMap) {
		this.sqlMap = sqlMap;
	}


	public Log getLog() {
		return log;
	}


	public void setLog(Log log) {
		this.log = log;
	}


	public SupplierBean getSupplierPickData() {
		return supplierPickData;
	}


	public void setSupplierPickData(SupplierBean supplierPickData) {
		this.supplierPickData = supplierPickData;
	}


	public Collection<SupplierBean> getSupplierList() {
		return supplierList;
	}


	public void setSupplierList(Collection<SupplierBean> supplierList) {
		this.supplierList = supplierList;
	}


	public HtmlExtendedDataTable getSupplierPickTable() {
		return supplierPickTable;
	}


	public void setSupplierPickTable(HtmlExtendedDataTable supplierPickTable) {
		this.supplierPickTable = supplierPickTable;
	}


	public SimpleSelection getSupplierPickSelection() {
		return supplierPickSelection;
	}


	public void setSupplierPickSelection(SimpleSelection supplierPickSelection) {
		this.supplierPickSelection = supplierPickSelection;
	}


	public CustomerBean getCustomerPickData() {
		return customerPickData;
	}


	public void setCustomerPickData(CustomerBean customerPickData) {
		this.customerPickData = customerPickData;
	}


	public Collection<CustomerBean> getCustomerList() {
		return customerList;
	}


	public void setCustomerList(Collection<CustomerBean> customerList) {
		this.customerList = customerList;
	}

	
	

	


	
	
}
